#ifndef __galileo_direct_texture_test_h__
#define __galileo_direct_texture_test_h__

#define LOG_TAG "galileo_direct_surface_texture_test"
#include <utils/Log.h>
#define GL_GLEXT_PROTOTYPES
#define EGL_EGLEXT_PROTOTYPES
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/stat.h>
#include <ui/DisplayInfo.h>
#include <ui/FramebufferNativeWindow.h>
#include <gui/ISurfaceComposer.h>
#include <gui/ISurfaceComposerClient.h>
#include <gui/SurfaceComposerClient.h>
#include <gui/SurfaceTexture.h>
#include <gui/SurfaceTextureClient.h>
#include <ui/GraphicBuffer.h>
#include <utils/String8.h>
#include <utils/threads.h>
#include <gui/Surface.h>

using namespace android;

#define STRINGIFY(A) #A

class Shaders {
public:
    Shaders();
    GLuint load();
    void destroy();
private:
    GLuint vertShaderNum;
    GLuint fragShaderNum;
    GLuint programHandle;
    static const char *vertexShaderSource;
    static const char *fragmentShaderSource;
    int compile(const char *shaderSource, GLuint ShaderNum);
};

class Renderer {
public:
    static int run(int argc, char *argv[]);
	void draw();
private:
    static volatile sig_atomic_t quit;
	static const EGLint gl_context_attribs[];
    static const EGLint s_configAttribs[];
    static const GLfloat position[][2];
    static const GLfloat texcoords[][2];
    static const GLfloat color[][3];
    sp<SurfaceControl> surfaceControl;
    sp<Surface> nativeSurface;
    sp<SurfaceComposerClient> surfaceComposerClient;
    sp<GraphicBuffer> textureBuffer;
    EGLSurface eglWindowSurface;
    EGLDisplay eglDisplay;
    EGLContext eglContext;
    EGLint displayWidth;
    EGLint displayHeight;
    Shaders shaders;
    GLuint programHandle;
    GLint locPosition;
    GLint locColor;
    GLint locTexcoord;
    GLint locSampler;
    GLuint texture;
    GLuint renderTargetTexture;
    GLuint fbo;
    EGLImageKHR image;
    Renderer();
    ~Renderer();
    void draw(sp<GraphicBuffer> buffer);
    void clear(sp<GraphicBuffer> buffer);
    void render();
    void init();
    void deinit();
    void handle_egl_error(const char *name);
    static void usage();
    static void sighandler(int signal);
};

#endif // __galileo_direct_texture_test_h__
